package  battle
{
	import Base.Loader.SWFLoader;
	
	import Common.GameDefine;
	import Common.RenderManager;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * ...
	 * @author 
	 */
	public class BattleData 
	{
		public var turn:int = 0;
		
		public var battleCommand:BattleCommand = null;
		public var battleResultCommand:BattleResultCommand = null;
		
		public var playerArray:Array = null;
		public var playerPetArray:Array = null;
		public var defenderArray:Array = null;
		public var defenderPetArray:Array = null;
		
		public var fightMC:MovieClip = null;
		public var inited:Boolean = false;
		public var loader:SWFLoader = null;
		
		public function BattleData() 
		{
			
		}
		
		public static var instance:BattleData = null;
		public static function Instance():BattleData
		{
			if ( instance == null )
			{
				instance = new BattleData();
			}
			
			return instance;
		}
		
		
		public function clear():void
		{
			if ( !fightMC )
			{
				return;
			}
			
			fightMC.visible = false;
			fightMC.Clear();
			battleCommand.clear();
			battleResultCommand.clear();
			
			while ( playerArray.length ) 
			{
				playerArray[0] = null;
				playerArray.shift();
			}
			
			while ( playerPetArray.length ) 
			{
				playerPetArray[0] = null;
				playerPetArray.shift();
			}
			
			while ( defenderArray.length ) 
			{
				defenderArray[0] = null;
				defenderArray.shift();
			}
			
			while ( defenderPetArray.length ) 
			{
				defenderPetArray[0] = null;
				defenderPetArray.shift();
			}
			
		}
		
		public function init():void
		{
			battleCommand = new BattleCommand();
			battleResultCommand = new BattleResultCommand();
			
			playerArray = new Array();
			playerPetArray = new Array();
			defenderArray = new Array();
			defenderPetArray = new Array();
			
			loader = new SWFLoader();
			loader.contentLoaderInfo.addEventListener( Event.INIT , onInit );
			loader.Load( GameDefine.PATHUI + "fight.swf" );
			
		}
		
		public function onInit( event:Event ):void
		{
			var cls:Class = loader.getClass( "FightMC" );
			fightMC = new cls();
			fightMC.visible = false;
			
			loader.contentLoaderInfo.removeEventListener( Event.INIT , onInit );
			loader.Release();
			
			inited = true;
			
			SceneManager.instance.sceneSprite.addChild( fightMC );
			
			for (var i:int = 0; i < fightMC.buttons.length; i++) 
			{
				fightMC.buttons[i].button.addEventListener( MouseEvent.CLICK , BattleSession.instance.onButtonClick );
			}
			
			fightMC.playerCommandMC.defenceButton.addEventListener( MouseEvent.CLICK , BattleSession.instance.onButtonClick );
			fightMC.playerCommandMC.escapeButton.addEventListener( MouseEvent.CLICK , BattleSession.instance.onButtonClick );
			
			if ( BattleSession.instance.type == BattleState.PVE )
			{
				BattleSession.instance.startFight( BattleSession.instance.pvp , BattleSession.instance.direction , BattleSession.instance.playerNum , BattleSession.instance.petNum , BattleSession.instance.anemyNum , BattleSession.instance.anemyPetNum );
			}
			
			resize();
		}
		
		public function resize():void
		{
			if ( fightMC )
			{
				fightMC.x = RenderManager.instance.stageWidth * 0.5 - fightMC.width * 0.5;
				fightMC.y = RenderManager.instance.stageHeight * 0.5 - fightMC.height * 0.5;
			}
		}
		
		public function release():void
		{
			battleCommand = null;
			battleResultCommand = null;
			
			playerArray = null;
			playerPetArray = null;
			defenderArray = null;
			defenderPetArray = null;
			
			fightMC = null;
		}
		
	}

}